import collections


class Solution(object):
    def maxNumOfSubstrings(self, s):
        d_min = {}
        d_max = {}
        d_count = collections.defaultdict(int)
        for index, c in enumerate(s):
            if c not in d_min:
                d_min[c] = index
            d_max[c] = index
            d_count[c] += 1
        st = []
        for key in d_min:
            st.append([d_min[key], d_max[key], key])
        st.sort()
        out = []
        m = len(st)
        for i in range(m):
            left_min, now_max, key = st[i]
            count = 0
            count += d_count[key]
            j = i + 1
            while count < now_max - left_min + 1 and j < m:
                if st[j][0] < now_max:
                    now_max = max(st[j][1], now_max)
                    count += d_count[st[j][2]]
                    j += 1
                else:
                    break
            if count == now_max - left_min + 1:
                out.append([left_min, now_max])
        ans = []
        out.sort(key=lambda x: x[1])
        for u, v in out:
            if not ans:
                ans.append([u, v])
            else:
                if u > ans[-1][1]:
                    ans.append([u, v])
        return [s[u: v + 1] for u, v in ans]


data = Solution()
s = "adefaddaccc"
print(data.maxNumOfSubstrings(s))
